package frontend.bean;


import java.awt.Component;
import java.io.Serializable;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;

import util.CommUtils;

public class BasicCombox extends JComboBox {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1554727439730222111L;

	
	 public BasicCombox() {
	        super();
	        setRenderer(new BasicComboxRenderer());
	    }

	    public void addItem(Object item) {
	        super.addItem(item);
	    }

	    public void setSelectedItem(Object obj) {
	        if (obj == null) {
	            super.setSelectedItem(null);
	            return;
	        }
	        
	        super.setSelectedItem(obj);
	    }
	    
	    public int getItemIndex(ComboBoxItem item){
	    	for(int i=0; i<this.getItemCount(); i++){
	    		Object o = getItemAt(i);
	    		if(o == item)
	    			return i;
	    	}
	    	return -1;
	    }
	    
	    public void moveItem(ComboBoxItem item, int idxTo){
	    	int idx = getItemIndex(item);
	    	if(idx < idxTo)
	    		idxTo--;
	    	removeItem(item);
	    	insertItemAt(item, idxTo);
	    }
	    
	    public void updateResource() {
	    	for ( int i = getItemCount() - 1; i>=0;i--) {
	    		ComboBoxItem item = (ComboBoxItem)getItemAt(i);
	    		removeItem(item);
	    		item.setName("test");
	    		insertItemAt(item, i);
	    	}
	    	validate();
		}
}

class BasicComboxRenderer implements ListCellRenderer, Serializable {
	private static final int ITEM_BORDER_LEFT_RIGHT = 6;
	private static final int ITEM_BORDER_TOP_BOTTOM = 1;
	private static Border unFocusBorder = new EmptyBorder(
			ITEM_BORDER_TOP_BOTTOM, ITEM_BORDER_LEFT_RIGHT,
			ITEM_BORDER_TOP_BOTTOM, ITEM_BORDER_LEFT_RIGHT);

	public BasicComboxRenderer() {
		super();
	}

	private JLabel createItemLable() {
		JLabel lbl = new JLabel();
		lbl.setOpaque(true);
		lbl.setBorder(unFocusBorder);
		return lbl;
	}

	public Component getListCellRendererComponent(JList list, Object value,
			int index, boolean isSelected, boolean cellHasFocus) {
		JLabel itemLbl = createItemLable();
		if (isSelected) {
			itemLbl.setBackground(list.getSelectionBackground());
			itemLbl.setForeground(list.getSelectionForeground());
		} else {
			itemLbl.setBackground(list.getBackground());
			itemLbl.setForeground(list.getForeground());
		}

		int maxWidth = list.getVisibleRect().width - ITEM_BORDER_LEFT_RIGHT * 2;
		if (value == null) {
			itemLbl.setText("");
			return itemLbl;
		} else if (value instanceof ComboBoxItem) {
			ComboBoxItem item = (ComboBoxItem) value;
			String lbl = item.getName();
			if (maxWidth > 0) {
				String text = CommUtils.getMaxWidthString(lbl, itemLbl,
						maxWidth);
				itemLbl.setText(text);
				if (!text.equals(lbl))
					itemLbl.setToolTipText(lbl);
				else
					itemLbl.setToolTipText(null);
			} else {
				itemLbl.setText(lbl);
			}

			// if (!item.isEnabled()) {
			// itemLbl.setBackground(list.getBackground());
			// itemLbl.setForeground(Color.gray);
			// }
		} else {
			itemLbl.setText(value.toString());
		}

		return itemLbl;
	}
}
